// CSE571 - player-main.cpp
// Authors: Jonathan Wald, Daniel Sarfati, Guangle Fan
// This file holds the main function that is run by the client program.

#include <iostream>
#include "stdio.h"

#include "ace/Auto_Ptr.h"

#include "ace/INET_Addr.h"
#include "ace/SOCK_Acceptor.h"
#include "ace/SOCK_Stream.h"
#include "ace/Signal.h"

#include "game-manager.h"
#include "player-game.h"
#include "player.h"
#include "signal-handler.h"
#include "sig-timer-handler.h"
#include "io-manager.h"
#include "msg-flag.h"
#include "client-interpreter.h"
#include "globals.h"

class PlayerSvcHandler;

extern char buffer[];

int main(int argc, char* argv[]) {

   GameManager* manager = GameManager::instance();
   manager->connector = new PlayerConnector(ACE_Reactor::instance());

   string player_name;
   bool saw_file_flag = false;
   
   const int NAME_FLAG = 0, FILE_FLAG = 1, AUTO_ACT_FLAG = 2;
   int flag;
    
   bool parse_error = false;
   string error_msg;
   
   // Process the command line switches.  Look at every second arg for
   // a recognizable flag.
   for (int i = 1; i < argc && !parse_error; i += 2) {
      if (!strcmp("-n", argv[i])) flag = NAME_FLAG;
      else if (!strcmp("-f", argv[i])) flag = FILE_FLAG;
      else if (!strcmp("-q", argv[i])) { //AUTO_ACT = true;
flag = AUTO_ACT_FLAG; }
      else {
         parse_error = true;
         error_msg = "Unknown flag.";
         break;
      }
      
      if (i != argc - 1) {
	if ((strlen(argv[i+1]) == 0) || (argv[i+1][0] == '-')) {
	  parse_error = true;
	  error_msg = "Missing parameter for flag.";
	  break;
	}
      }
    
      switch (flag) {
      case NAME_FLAG:
	player_name = argv[i+1];
	if (player_name.size() == 0) {
	  parse_error = true;
	  error_msg = "Player must have name.";
	}
	else manager->set_name(player_name);
	break;
      case FILE_FLAG:
	saw_file_flag = true;
	if (manager->init(1, &(argv[i+1])) != 0) { 
	  parse_error = true; 
	  error_msg = "Could not parse defintion file.";
	}
	break;
      case AUTO_ACT_FLAG:
	manager->set_auto_act();
	break;
      default:
	parse_error = true;
	error_msg = "Missing flag.";
	break;
      }
   }
   
   if (!saw_file_flag) {
      parse_error = true;
      error_msg = "Must enter a definition file.";
   }
   
   if (parse_error) {
      std::cout << "ERROR: " << error_msg << std::endl;
      std::cout << "Usage: client -n <player name> -f <definition file> [-q] " << std::endl;
      return -1;
   }
   
   // Kill the client if no connections were able to be opened
   if (manager->num_games() == 0) return -1; 

   // // Implement signal handler.                                                
   SigHandler sh (ACE_Reactor::instance());

   //Implement timer to send quit messages.
   PlayerSigTimerHandler pst (ACE_Reactor::instance());
  
   IOManager* io_manager = IOManager::instance();
   io_manager->open();

   GameManager::instance()->interpreter.activate(THR_NEW_LWP|THR_DETACHED, 1);

   while (!SigHandler::sigint_fired() || !IOManager::instance()->shut_down) {
      ACE_Reactor::instance()->handle_events();
   }

   if (GameManager::instance() != NULL) {
       delete GameManager::instance();
   }

   // Leave pipe open for 2 seconds so that server can process termination.
   sleep(3);
   delete io_manager;
   return 0;
}

